package top.kaoshanji.ch05;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.sql.*;
/**
 * 代码清单 ch05-01：连接到数据库示例(5.3.5节)
 * 执行看到结果之后手动将表 Greetings 删除，以便后续继续执行
 * author kaoshanji
 * ^_^
 * create time 2024/12/8 18:58
 */
public class TestDB {

    private static final Logger logger = LogManager.getLogger(TestDB.class);

    public static void main(String[] args) throws SQLException, IOException {
        logger.info("...代码清单 ch05-01：连接到数据库示例(5.3.5节)...");

        runTest();

    }

    public static void runTest() throws SQLException, IOException {

        try (Connection connection = DBUtil.getConnection(); Statement statement = connection.createStatement()){
            statement.executeUpdate("CREATE TABLE Greetings (Message CHAR(20))");
            statement.executeUpdate("INSERT INTO Greetings VALUES ('Hello, World!')");

            try (ResultSet result = statement.executeQuery("SELECT * FROM Greetings")){
                if (result.next()) {
                    System.out.println(result.getString(1));
                }
            }

            // statement.executeUpdate("DROP TABLE Greetings");

        }

    }




}
